package com.tuanzi.loan.web.bean;

import static com.tuanzi.loan.business.constants.DictionaryCode.WORKFLOW_CATEGORY;

import java.io.IOException;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.bean.ViewScoped;

import org.primefaces.model.LazyDataModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.google.common.collect.Lists;
import com.tuanzi.loan.core.aspect.Debug;
import com.tuanzi.loan.web.configuration.JSFBean;
import com.tuanzi.loan.web.lazy.system.DoneTaskLazyDataModel;
import com.tuanzi.loan.web.lazy.system.StartableWorkflowLazyDataModel;
import com.tuanzi.loan.web.lazy.system.TodoTaskLazyDataModel;
import com.tuanzi.loan.workflow.entity.FormBinding;
import com.tuanzi.loan.workflow.service.FormBindingService;
import com.tuanzi.loan.workflow.service.TodoReadLogService;
import com.tuanzi.loan.workflow.service.WorkflowProcessorService;
import com.tuanzi.loan.workflow.vo.*;

import lombok.Data;
import lombok.EqualsAndHashCode;

/**
 * @author Ray 2018/01/22
 */
@Data
@Debug
@Component
@ViewScoped
@EqualsAndHashCode(callSuper = true)
public class WorkbenchBean extends JSFBean {

	private static final long serialVersionUID = 3019835637214096291L;

	@Autowired
	private WorkflowProcessorService workflowProcessorService;
	@Autowired
	private FormBindingService formBindingService;
	@Autowired
	private TodoReadLogService todoReadLogService;

	private LazyDataModel<TaskSearchResponse> todoTaskLazyDataModel;
	private LazyDataModel<TaskSearchResponse> doneTaskLazyDataModel;
	private LazyDataModel<ProcessDefinitionResponse> startableWorkflowLazyDataModel;
	private List<TaskSearchResponse> startableWorkflows;

	private TaskSearchRequest startableWorkflowSearchRequest;
	private TaskSearchRequest todoTaskSearchRequest;
	private TaskSearchRequest doneTaskSearchRequest;

	private List<TrackingResponse> trackingResponses;
	private String procInstId;
	private String deploymentId;

	@PostConstruct
	private void init() {
		initTodoTaskLazyDataModel();
		initDoneTaskLazyDataModel();
		initStartableWorkflowLazyDataModel();
		trackingResponses = Lists.newArrayList();
	}

	public String onHandleWorkflow(String taskId) {
		TaskResponse taskResponse = workflowProcessorService.findTask(taskId);
		FormBinding formBinding = formBindingService.findByProcDefIdAndActId(taskResponse.getProcDefId(), taskResponse.getTaskDefinitionKey());

		if (formBinding == null) {
			addErrorMessage("该流程环节没有绑定表单");
			return null;
		}

		todoReadLogService.read(taskResponse.getProcInstId(), taskResponse.getId());
		return redirect(formBinding.getFormUrl() + "?taskId=" + taskId);
	}

	public void onResetTodoTask() {
		initTodoTaskLazyDataModel();
	}

	public void onResetDoneTask() {
		initDoneTaskLazyDataModel();
	}

	public void onResetStartableWorkflow() {
		initStartableWorkflowLazyDataModel();
	}

	public String onGoToStartPageUrl(String startPageUrl) {
		return redirect(startPageUrl);
	}

	public void onLoadProcessTracking(String procInstId) throws IOException {
		trackingResponses = workflowProcessorService.findProcessTrackingByProcInstId(procInstId);
	}

	private void initStartableWorkflowLazyDataModel() {
		startableWorkflowSearchRequest = new TaskSearchRequest();
		startableWorkflowLazyDataModel = new StartableWorkflowLazyDataModel(startableWorkflowSearchRequest);
	}

	private void initTodoTaskLazyDataModel() {
		todoTaskSearchRequest = new TaskSearchRequest();
		todoTaskLazyDataModel = new TodoTaskLazyDataModel(todoTaskSearchRequest);
	}

	private void initDoneTaskLazyDataModel() {
		doneTaskSearchRequest = new TaskSearchRequest();
		doneTaskLazyDataModel = new DoneTaskLazyDataModel(doneTaskSearchRequest);
	}

	@Override
	protected List<String> getDictionaryCodes() {
		return Lists.newArrayList(WORKFLOW_CATEGORY);
	}
}
